using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using UserSurface;
using ApplicationCore;
using ZedGraph;

namespace EnzymeLaborTool
{
    public partial class StatisticsForm : Form
    {

        MainSurface ms;


        public StatisticsForm(MainSurface ms)
        {
            this.ms = ms;
            InitializeComponent();
        //    ms.FillAssayQualityControl();
            enzymeCodesBindingSource.CurrentChanged +=new EventHandler(enzymeCodesBindingSource_CurrentChanged);
         //   enzymeCodesBindingSource.DataSource = ms.Data.EnzymeCodes;
           
            
        }

        private List<BatchErrorPair> ListRows(string enzyme)
        {

            List<BatchErrorPair> ret = new List<BatchErrorPair>();
            //SortedDictionary<string, List<double?>> temp = new SortedDictionary<string, List<double?>>();
            //EnzymeProcessingDataSet.AllAssaysControlValuesRow[] rows = (EnzymeProcessingDataSet.AllAssaysControlValuesRow[])ms.Data.AllAssaysControlValues.Select("Enzyme_Fk = '" + enzyme + "'", "BatchDate_Fk");
            //foreach (EnzymeProcessingDataSet.AllAssaysControlValuesRow r in rows)
            //{
            //    string batname = r.BatchName_Fk + "-" + r.BatchNumber_Fk + "-" + r.BatchDate_Fk.ToShortDateString();
            //    if (temp.ContainsKey(batname))
            //    {
            //        temp[batname].Add(r.Value);
            //    }
            //    else
            //    {
            //        temp.Add(batname, new List<double?>());
            //        temp[batname].Add(r.Value);
            //    }
            //}
            //foreach (string s in temp.Keys)
            //{
            //    BatchErrorPair bep = new BatchErrorPair();
            //    bep.batch = s;
            //    bep.error = (double)Statistics.Error(temp[s].ToArray());
            //    ret.Add(bep);
            //}

            return ret;
        }



        private void enzymeCodesBindingSource_CurrentChanged(object sender, EventArgs e)
        {
            //string enz = ((enzymeCodesBindingSource.Current as DataRowView).Row as EnzymeProcessingDataSet.EnzymeCodesRow).Enzyme;
            //ListRows(enz);
            //List<BatchErrorPair> batchErrors = ListRows(enz);
            //CreateChart(this.zedGraphControl1, enz, batchErrors);
        }

        // Call this method from the Form_Load method, passing your ZedGraphControl
        private void CreateChart(ZedGraphControl zgc, string enzyme, List<BatchErrorPair> batchErrors)
        {
           GraphPane myPane = zgc.GraphPane;
           //myPane.FontSpec = new FontSpec("Verdana", 10, Color.Black, false, false, false);
           // GraphPane myPane = new GraphPane();

            myPane.CurveList.Clear();
            // Set the title and axis labels
            myPane.Title = enzyme;
            myPane.XAxis.Title = "batches";
            myPane.YAxis.Title = "error %";
            myPane.XAxis.TitleFontSpec.FontColor = Color.OrangeRed;
            myPane.YAxis.TitleFontSpec.FontColor = Color.OrangeRed;
            myPane.YAxis.MaxAuto = false;
            myPane.YAxis.Max = 101;

            List<double> errors = new List<double>();
            List<double?> errors2 = new List<double?>();
            List<double> xdummy = new List<double>();
            List<string> labels = new List<string>();

            

            for (int i = 0; i < batchErrors.Count; i++)
            {
                if (!Double.IsNaN(batchErrors[i].error))
                {
                    errors2.Add(batchErrors[i].error);
                    errors.Add(Math.Min(batchErrors[i].error,101f));
                    xdummy.Add(i);
                    labels.Add(batchErrors[i].batch);
                }
            }

           
            double[] avgY;
            double[] avgX;
            double[] medY;
  
            double avg = Double.NaN;
            double med = Double.NaN;
            if (errors2.Count > 0)
            {
                avg = Math.Round((double)Statistics.Avg(errors2.ToArray()));
                med = Math.Round((double)Statistics.Median(errors2.ToArray()));
                List<double> temp = new List<double>();
                List<double> temp2 = new List<double>();
                for (int i = 0; i < errors.Count; i++)
                {
                    temp.Add(avg);
                    temp2.Add(med);
                }
                avgY = temp.ToArray();
                medY = temp2.ToArray();
                avgX = xdummy.ToArray();
                avgX[0] = -1;
            }
            else
            {
                avgY = new double[2] { 0, 0 };
                medY = new double[2] { 0, 0 };
                avgX = new double[2] { 0, xdummy.Count - 1 };
            }

           myPane.XAxis.Type = AxisType.Text;
           myPane.XAxis.TextLabels = labels.ToArray();
           myPane.XAxis.Step = 1f;
           myPane.XAxis.ScaleFontSpec.Angle = 90f;
           myPane.XAxis.ScaleFontSpec.Size = 10;
           myPane.XAxis.Scale.StepAuto = false;

           myPane.AddCurve("Mean(" + avg +")", avgX, avgY, Color.Red,SymbolType.None);
           myPane.AddCurve("Median(" + med + ")", avgX, medY, Color.Blue, SymbolType.None);
           myPane.AddBar("Error in controls", xdummy.ToArray(), errors.ToArray(), Color.OrangeRed);

           myPane.AxisFill = new Fill(Color.White, Color.FromArgb(255, 255, 166), 45.0F);

           zgc.AxisChange();
           zgc.Refresh();
            
        }
    }


    struct BatchErrorPair
    {
        public double error;
        public string batch;
    }
    // struct BatchRow
    //{
    //    public double value;
    //    public string name;
    //    public int number;
    //    public DateTime date;
    //}


}